#include "frm_control_test.h"
using namespace frm;
using namespace control;
using namespace filler;
using namespace cursor;
using namespace test;

// TPanelA ////////////////////////////////////////////////////////////////////

//
void TPanelA::TComponent_Paint(TPaint &Paint,int NPaintStack)
{
  TPanel::TComponent_Paint(Paint,NPaintStack); if (Focus_IsEnter()) {
    TComponent_SetCaretByPos(Paint.GRect,Focus_GetPos());
  }
}

//
void TPanelA::TComponent_SetCaretByX(TGeometryRect &GRect,int XClient)
{
}

//
void TPanelA::TComponent_SetCaretByPos(TGeometryRect &GRect,int Pos)
{
  grf::TCaret &Caret = GetFocus().Caret;
  Caret.RClient = GRect.RThisVisible;
    //Caret.RClient.Left  = GRect.RThis.Left;
    Caret.RClient.Width = 2;
    Caret.RClient.Left += 2;
  Caret.Hide();
  Caret.Update();
  Caret.Show();
}

//
void TPanelA::TComponent_Mouse(TMouse Mouse)
{
  if (!Mouse.IsProcessed) switch (Mouse.State)
  {
    case grf::TMouse::EState_LButtonDown:
    //case grf::TMouse::EState_LButtonDblClick:
    {  
      Mouse.IsProcessed = true;
      if (Mouse.Shift.Contains(grf::TShift::Ctrl)) {
        UpdateVisible(false);
        break;
      }  
      Mouse_SetCapture(); if (Focus_Enable()) {
        Focus_Enter();
      }
    }
    break;

    case grf::TMouse::EState_LButtonUp:
    {
      Mouse.IsProcessed = true;
      Mouse_ReleaseCapture();
    }
    break;

    case grf::TMouse::EState_RButtonDown:
    {
      Mouse.IsProcessed = true;

      svl::TCommand *Cmd;
      svl::TCommandArray *Items = new svl::TCommandArray;
        Items->Add(Cmd = new svl::TCommand("C1,C2"));
          Cmd->Children.Add(new TCommand1);
          Cmd->Children.Add(new TCommand2);
      TPopupMenu::Popup(GetWindow(),Mouse.PtClient,Items);

/**********
      TCommand1 Cmd1,Cmd2;
      svl::TCommandArray Items;
        Items.Add(&Cmd1,false);
          Cmd1.Children.Add(&Cmd2,false);
      TPopupMenu::Popup(GetWindow(),Mouse.PtClient,&Items,false);
************/      
      //::MessageBeep(0);
    }
    break;
  }
  TPanel::TComponent_Mouse(Mouse);
}

//
void TPanelA::TComponent_MouseEnter() 
{ 
  TPanel::TComponent_MouseEnter(); 
  if (!Focus_Enable()) { 
    UpdateStyle(style->MouseEnter); 
  } 
}

//
void TPanelA::TComponent_MouseExit() 
{ 
  TPanel::TComponent_MouseExit(); 
  if (!Focus_Enable()) { 
    UpdateStyle(style->Normal); 
  } 
}

//
void TPanelA::TComponent_FocusEnter() 
{ 
  TPanel::TComponent_FocusEnter(); 
  UpdateStyle(style->MouseEnter); 
}

//
void TPanelA::TComponent_FocusExit() 
{ 
  TPanel::TComponent_FocusExit();
  UpdateStyle(style->Normal); 
}

//
void TPanelA::TComponent_KeyboardKey(grf::TKeyboardKey &KeyboardKey)
{
  if (!KeyboardKey.IsProcessed && KeyboardKey.State == KeyboardKey.EState_Down) {
    switch (KeyboardKey.Key) {
      case VK_DELETE:
        KeyboardKey.IsProcessed = true; 
        PostDeleteThis();
      break;
    
      case VK_INSERT:
        KeyboardKey.IsProcessed = true;

        struct TStyle : public TPanelA::TStyle
        {
          TStyle()
          {
            Normal.Filler.Set(TColorLevelFrame(grf::TColor(255,255,255)));
            Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
            
            MouseEnter.Filler.Set(TColorLevelFrame(grf::TColor(255,0,0)));
            MouseEnter.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
          }
        };
        static TStyle Style;

        TPanelA *C = new TPanelA(GetWindow());
          C->GetGeometry().SetWidth (new geometry::TFixed(50));
          C->GetGeometry().SetHeight(new geometry::TFixed(50));
          C->SetStyle(Style);
        
        static_cast<TPanel*>(GetParent())->AddChildBefore(C,this);
        C->Focus_Enter();
      break;
    }
  }
  TPanel::TComponent_KeyboardKey(KeyboardKey);
}

//
void TPanelA::TComponent_MouseWheel(TMouseWheel MouseWheel)
{ 
  if (!MouseWheel.IsProcessed && EnScroll) {
    MouseWheel.IsProcessed = true;

    if (MouseWheel.Shift.Contains(grf::TShift::Ctrl)) {
      grf::TPoint Pt = GetScrollChildren(); 
      Pt.X += (MouseWheel.Delta > 0 ? -10 : +10);
      UpdateScrollChildren(Pt);
    }
    else {
      grf::TPoint Pt = GetScrollChildren(); 
      Pt.Y += (MouseWheel.Delta > 0 ? -10 : +10);
      UpdateScrollChildren(Pt);
    }
  } 
  TPanel::TComponent_MouseWheel(MouseWheel);
}

// TPanelWin //////////////////////////////////////////////////////////////////

//
TPanelWin::TPanelWin(control::TWindow &Window) : control::TPanel(Window) 
{
  Filler.Set(TArray()
   .Add(TColor(255,255,255))
   .Add(TLineVertCenter(192,192,192))
   .Add(TLineHorzCenter(192,192,192))
  );
  
  Cursor.Set(TCursorWin(IDC_CROSS));
  grf::TWindow::SetParent(Window);

  //Win.SetWindowRect(grf::TRect(100,100,600,600),false);
  //Win.Show();

}

//
void TPanelWin::TComponent_Paint(TPaint &Paint,int NPaintStack)
{
  if (IsVisibleInParents()) {
    grf::TWindow::SetWindowRect(Paint.GRect.RThis,true);
    grf::TWindow::Show();
  }
  else {
    Hide();
  }
//  TPanel::TComponent_Paint(Paint,NPaintStack); if (Focus_IsEnter()) {
//    TComponent_SetCaretByPos(Paint.GRect,Focus_GetPos());
//  }
}

//
void TPanelWin::TWindow_Paint(grf::TPaint &grfPaint)
{
  grf::TRect ClientRect = GetClientRect();
  TComponent Component;
  TPaint     Paint;

  Paint.State        = TPaint::EState_PaintAll;
  Paint.DC           = grfPaint.DC;
  Paint.RClientPaint = grfPaint.DC.RClientPaint;

  Paint.GRect.RParent = ClientRect;
  Paint.GRect.RParentVisible = ClientRect;

  Paint.GRect.RThis = ClientRect;
  Paint.GRect.RThisVisible = ClientRect;

  Filler.Paint(Component,Paint);
}

//
void TPanelWin::TWindow_Mouse(grf::TMouse &Mouse) 
{
  Cursor.Paint();
}

// TWindowTest ////////////////////////////////////////////////////////////////

//
TWindowTest::TWindowTest()
{
  grf::TClientDC DC(*this);
	int s = ::GetDeviceCaps(DC, HORZSIZE);
	int a = ::GetDeviceCaps(DC, HORZRES);
	double Scale = (double)a / (10*s);
	Scale = 1/Scale;

  //SetScale(( 1/K) );
/****             
  svl::TCommand *Cmd;
  mainMenu.Items.Add(Cmd = new svl::TCommand("C1"));
    Cmd->Children.Add(new TCommand1);
  mainMenu.Items.Add(Cmd = new svl::TCommand("C2"));
    Cmd->Children.Add(new TCommand2);
    Cmd->Children.Add(new TCommand1);
  mainMenu.SetToWindow(*this);
********/

  Client.GetGeometry().SetDirect(new geometry::THorz());
  Client.GetGeometry().SetWAlign(new geometry::TCenter());
  Client.GetGeometry().SetHAlign(new geometry::TCenter());

  struct TStyle1 : public TPanelA::TStyle
  {
    TStyle1()
    {
      Normal.Filler.Set(TColorLevelFrame(grf::TColor(0,255,255)));
      Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
      
      MouseEnter.Filler.Set(TColorLevelFrame(grf::TColor(255,0,0)));
      MouseEnter.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
    }
  };
  static TStyle1 Style1;
  
  struct TStyle2 : public TPanelA::TStyle
  {
    TStyle2()
    {
      Normal.Filler.Set(TArray()
       .Add(TFrameRect(0,255,255))
       .Add(TLineVertCenter(192,192,192))
       .Add(TLineHorzCenter(192,192,192))
      );
      Normal.Cursor.Set(cursor::TCursorWin(IDC_CROSS));
      
      MouseEnter.Filler.Set(TArray()
       .Add(TFrameRect(255,0,0))
       .Add(TLineVertCenter(192,192,192))
       .Add(TLineHorzCenter(192,192,192))
      );
      MouseEnter.Cursor.Set(cursor::TCursorWin(IDC_CROSS));
    }
  };
  static TStyle2 Style2;

  struct TStyle3 : public TPanelA::TStyle
  {
    TStyle3()
    {
      Normal.Filler.Set(TColor(0,200,0));
      Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
      
      MouseEnter.Filler.Set(TColor(0,250,0));
      MouseEnter.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
    }
  };
  static TStyle3 Style3;
  
  struct TStyle4 : public TPanelA::TStyle
  {
    TStyle4()
    {
      Normal.Filler.Set(TColorLevelFrame(grf::TColor(0,255,255)));
      Normal.Cursor.Set(cursor::TCursorWin(IDC_CROSS));
      
      MouseEnter.Filler.Set(TColorLevelFrame(grf::TColor(255,0,0)));
      MouseEnter.Cursor.Set(cursor::TCursorWin(IDC_CROSS));
    }
  };
  static TStyle4 Style4;
  
  TPanel *ClientScale = new TPanel(*this);
    ClientScale->SetScale(Scale);
    ClientScale->GetGeometry().SetWidth (new geometry::TParent());
    ClientScale->GetGeometry().SetHeight(new geometry::TParent());
    ClientScale->GetGeometry().SetDirect(new geometry::THorz());
    ClientScale->GetGeometry().SetWAlign(new geometry::TCenter());
    ClientScale->GetGeometry().SetHAlign(new geometry::TCenter());
  Client.AddChildLast(ClientScale);
  
  TPanelA *C1,*C2;
  TPanelWin *W1;

  C1 = new TPanelA(*this);
    C1->GetGeometry().SetWidth (new geometry::TFixed(200));
    C1->GetGeometry().SetHeight(new geometry::TFixed(200));
    C1->GetGeometry().SetDirect(new geometry::THorz());
    C1->GetGeometry().SetWAlign(new geometry::TCenter());
    C1->GetGeometry().SetHAlign(new geometry::TCenter());
    C1->EnFocus = false;
    C1->SetStyle(Style1);
  ClientScale->AddChildLast(C1);
  
    W1 = new TPanelWin(*this);
      W1->GetGeometry().SetWidth (new geometry::TFixed(100));
      W1->GetGeometry().SetHeight(new geometry::TFixed(100));
      W1->GetGeometry().SetDirect(new geometry::THorz());
      W1->GetGeometry().SetWAlign(new geometry::TCenter());
      W1->GetGeometry().SetHAlign(new geometry::TCenter());
    C1->AddChildLast(W1);
    
  C1 = new TPanelA(*this);
    C1->GetGeometry().SetWidth (new geometry::TFixed(250));
    C1->GetGeometry().SetHeight(new geometry::TParent());
    C1->GetGeometry().SetDirect(new geometry::TVert());
    C1->GetGeometry().SetWAlign(new geometry::TCenter());
    C1->GetGeometry().SetHAlign(new geometry::TJustify());
    C1->SetStyle(Style2);
  ClientScale->AddChildLast(C1);

  for (int N = 0; N < 5; N++)
  {
    C2 = new TPanelA(*this);
      C2->GetGeometry().SetWidth (new geometry::TFixed(30));
      C2->GetGeometry().SetHeight(new geometry::TFixed(50));
      C2->GetGeometry().SetDirect(new geometry::THorz());
      C2->GetGeometry().SetWAlign(new geometry::TCenter());
      C2->GetGeometry().SetHAlign(new geometry::TCenter());
      C2->SetStyle(Style1);
    C1->AddChildLast(C2);
  }
  
  C1 = new TPanelA(*this);
    C1->GetGeometry().SetWBorder(new geometry::TRuleBorder(20,50));
    C1->GetGeometry().SetHBorder(new geometry::TRuleBorder(20,50));

    C1->GetGeometry().SetWidth (new geometry::TFixed(300));
    C1->GetGeometry().SetHeight(new geometry::TFixed(450));
    C1->GetGeometry().SetDirect(new geometry::TVert());
    C1->GetGeometry().SetWAlign(new geometry::TCenter());
    C1->GetGeometry().SetHAlign(new geometry::TCenter());
    C1->SetStyle(Style3);
    C1->EnScroll = true;
  ClientScale->AddChildLast(C1);

    C2 = new TPanelA(*this);
      C2->GetGeometry().SetWidth (new geometry::TFixed(50));
      C2->GetGeometry().SetHeight(new geometry::TFixed(50));
      C2->GetGeometry().SetDirect(new geometry::THorz());
      C2->GetGeometry().SetWAlign(new geometry::TCenter());
      C2->GetGeometry().SetHAlign(new geometry::TCenter());
      C2->SetStyle(Style4);
    C1->AddChildLast(C2);
}

///////////////////////////////////////////////////////////////////////////////

//                
void frm::control::test::Test()
{
  TWindowTest Win;
  
  Win.SetWindowRect(grf::TRect(100,100,600,600),false);
  Win.Show();
  Win.MessagesLoop();
}
